using UnityEngine;
//using static ICSharpCode.SharpZipLib.Zip.ExtendedUnixData;

public class MyJoystick : MonoBehaviour
{
	public static bool isDragging;

	public float radius;

	public float radius_updownup;

	private float inverseRadius;

	private float inverseSqrRadius;

	private Vector3 originPos;

	private float currentDistance;

	private Vector3 temPos;

	private Vector2 screenScale;

	private Vector2 inverseScreenScale;

	public UISprite spriteJoystickBg;

	public UISprite spriteJoysitck;

	private int moment;

	private float timeAvailableToNextMoment_Max = 0.5f;

	private float timeToNextMoment;

	private Color colorPress = new Color(1f, 1f, 1f, 0.5f);
	public bool isDownDown;
	public bool isUpDown;
	public bool isLeftDown;
	public bool isRightDown;
	public bool isDonwUp;
	public bool isLeftUp;
	public bool isRightUp;
	public bool isUpUp;

	private void Awake()
	{
		MathHelper.fitXPos(spriteJoystickBg);
	}

	private void Start()
	{
		originPos = base.transform.localPosition;
		screenScale = new Vector2((float)Screen.width / 800f, (float)Screen.height / 480f);
		inverseScreenScale.x = 1f / screenScale.x;
		inverseScreenScale.y = 1f / screenScale.y;
		inverseRadius = 1f / radius;
		inverseSqrRadius = inverseRadius * inverseRadius;
		isDragging = false;
		isDownDown = false;
		isUpDown = false;
		isLeftDown = false;
		isRightDown = false;
		isDonwUp = true;
		isLeftUp = true;
		isRightUp = true;
		isUpUp = true	;
	
	}

	private void Update()
	{
		//if (Global.isAndroid && UnityEngine.Input.touchCount <= 0)
		//{
		//	resetToCenterPos();
		//}
	}

	public void resetToCenterPos()
	{
		if (base.transform.localPosition != originPos)
		{
			timeToNextMoment = 0f;
			moment = 0;
			isDragging = false;
			base.transform.localPosition = originPos;
			spriteJoysitck.color = Color.white;
		}
	}

	private void FixedUpdate()
	{
		//if (!Global.isAndroid)
		//{
			handleInWindows();
		//}
		if (isDragging)
		{
			Instance<Court>.instance.teamUser.onDragging(getNormalizeSpeed(), getDirection());
			checkUpDownUp();
		}
	}

	public void wxOnDragStart()
	{
		if (!Instance<UIGame>.instance.isLockJoystick)
		{
			isDragging = true;
			Instance<Court>.instance.teamUser.onDragStart();
			spriteJoysitck.color = colorPress;
		}
	}

	public void wxOnDrag(Vector2 delta)
	{
		if (!Instance<UIGame>.instance.isLockJoystick)
		{
			temPos = base.transform.localPosition;
			temPos = new Vector3(temPos.x + delta.x * inverseScreenScale.x, temPos.y + delta.y * inverseScreenScale.y, 0f);
			currentDistance = Vector2.SqrMagnitude(temPos - originPos);
			if (currentDistance > radius * radius)
			{
				temPos -= originPos;
				Vector3 normalized = temPos.normalized;
				base.transform.localPosition = normalized * radius + originPos;
			}
			else
			{
				base.transform.localPosition = temPos;
			}
			isDragging = true;
			spriteJoysitck.color = colorPress;
		}
	}

	public void wxOnDragEnd()
	{
		if (!Instance<UIGame>.instance.isLockJoystick)
		{
			timeToNextMoment = 0f;
			moment = 0;
			isDragging = false;
			base.transform.localPosition = originPos;
			Instance<Court>.instance.teamUser.onDragEnd();
			spriteJoysitck.color = Color.white;
		}
	}

	private float getNormalizeSpeed()
	{
		return Vector2.SqrMagnitude(base.transform.localPosition - originPos) * inverseSqrRadius;
	}

	private Vector3 getDirection()
	{
		Vector3 vector = base.transform.localPosition - originPos;
		float y = vector.y;
		Vector3 vector2 = originPos - base.transform.localPosition;
		Vector3 vector3 = new Vector3(y, 0f, vector2.x);
		return vector3.normalized;
	}
	public void DownButtonDown()
	{
		isDownDown = true;
		isDonwUp = false;
	}
	public void DownButtonUp()
	{
		isDownDown = false;
		isDonwUp = true;
	}
	public void UpButtonDown()
	{
		isUpDown = true;
		isUpUp = false;
	}
	public void UpButtonUp()
	{
		isUpDown = false;
		isUpUp = true;
	}
	public void LeftButtonDown()
	{
		isLeftDown = true;
		isLeftUp = false;
	}
	public void LeftButtonUp()
	{
		isLeftDown = false;
		isLeftUp = true;
	}
	public void RightButtonDown()
	{
		isRightDown = true;
		isRightUp = false;
	}
	public void RightButtonUp()
	{
		isRightDown = false;
		isRightUp = true;
	}
	private void handleInWindows()
	{
		if (Instance<UIGame>.instance.isLockJoystick)
		{
			return;
		}
		if (!isDragging)
		{
			return;
		}
		bool flag = isDragging;
		isDragging = false;
		if (UnityEngine.Input.GetKey(KeyCode.UpArrow)||isUpDown)
		{
			isDragging = true;
			temPos = base.transform.localPosition;
			temPos = new Vector3(temPos.x, temPos.y + 100f * inverseScreenScale.y, 0f);
			currentDistance = Vector2.SqrMagnitude(temPos - originPos);
			if (currentDistance > radius * radius)
			{
				temPos -= originPos;
				Vector3 normalized = temPos.normalized;
				base.transform.localPosition = normalized * radius + originPos;
			}
			else
			{
				base.transform.localPosition = temPos;
			}
			if (!flag)
			{
				wxOnDragStart();
			}
		}
		if (UnityEngine.Input.GetKey(KeyCode.DownArrow)||isDownDown)
		{
			isDragging = true;
			temPos = base.transform.localPosition;
			temPos = new Vector3(temPos.x, temPos.y - 100f * inverseScreenScale.y, 0f);
			currentDistance = Vector2.SqrMagnitude(temPos - originPos);
			if (currentDistance > radius * radius)
			{
				temPos -= originPos;
				Vector3 normalized2 = temPos.normalized;
				base.transform.localPosition = normalized2 * radius + originPos;
			}
			else
			{
				base.transform.localPosition = temPos;
			}
			if (!flag)
			{
				wxOnDragStart();
			}
		}
		if (UnityEngine.Input.GetKey(KeyCode.LeftArrow)||isLeftDown)
		{
			isDragging = true;
			temPos = base.transform.localPosition;
			temPos = new Vector3(temPos.x - 100f * inverseScreenScale.x, temPos.y, 0f);
			currentDistance = Vector2.SqrMagnitude(temPos - originPos);
			if (currentDistance > radius * radius)
			{
				temPos -= originPos;
				Vector3 normalized3 = temPos.normalized;
				base.transform.localPosition = normalized3 * radius + originPos;
			}
			else
			{
				base.transform.localPosition = temPos;
			}
			if (!flag)
			{
				wxOnDragStart();
			}
		}
		if (UnityEngine.Input.GetKey(KeyCode.RightArrow)||isRightDown)
		{
			isDragging = true;
			temPos = base.transform.localPosition;
			temPos = new Vector3(temPos.x + 100f * inverseScreenScale.x, temPos.y, 0f);
			currentDistance = Vector2.SqrMagnitude(temPos - originPos);
			if (currentDistance > radius * radius)
			{
				temPos -= originPos;
				Vector3 normalized4 = temPos.normalized;
				base.transform.localPosition = normalized4 * radius + originPos;
			}
			else
			{
				base.transform.localPosition = temPos;
			}
			if (!flag)
			{
				wxOnDragStart();
			}
		}
		if (flag && !isDragging)
		{
			wxOnDragEnd();
		}
		if (!isDragging)
		{
			base.transform.localPosition = originPos;
		}
	}

	private void checkUpDownUp()
	{
		if (timeToNextMoment >= 0f)
		{
			if (timeToNextMoment > 0f)
			{
				timeToNextMoment -= Time.fixedDeltaTime;
			}
			switch (moment)
			{
			case 0:
				if (currentDistance > radius * radius * 0.5f && Mathf.Abs(temPos.x - originPos.x) < radius_updownup)
				{
					if (temPos.y > originPos.y)
					{
						moment = 1;
					}
					else
					{
						moment = -1;
					}
					timeToNextMoment = timeAvailableToNextMoment_Max;
				}
				break;
			case 1:
				if (currentDistance > radius * radius * 0.5f && Mathf.Abs(temPos.x - originPos.x) < radius_updownup && temPos.y < originPos.y)
				{
					moment = 2;
					timeToNextMoment = timeAvailableToNextMoment_Max;
				}
				break;
			case 2:
				if (currentDistance > radius * radius * 0.5f && Mathf.Abs(temPos.x - originPos.x) < radius_updownup && temPos.y > originPos.y)
				{
					moment = 3;
				}
				break;
			case -1:
				if (currentDistance > radius * radius * 0.5f && Mathf.Abs(temPos.x - originPos.x) < radius_updownup && temPos.y > originPos.y)
				{
					moment = -2;
					timeToNextMoment = timeAvailableToNextMoment_Max;
				}
				break;
			case -2:
				if (currentDistance > radius * radius * 0.5f && Mathf.Abs(temPos.x - originPos.x) < radius_updownup && temPos.y < originPos.y)
				{
					moment = -3;
				}
				break;
			}
		}
		else
		{
			timeToNextMoment = 0f;
			moment = 0;
		}
		if (Mathf.Abs(temPos.x - originPos.x) >= radius_updownup)
		{
			timeToNextMoment = 0f;
			moment = 0;
			Instance<Court>.instance.teamUser.isUpDownUping = false;
		}
		if (moment == 3 || moment == -3)
		{
			Instance<Court>.instance.teamUser.onUpDownUp();
			moment = 0;
			timeToNextMoment = 0f;
		}
	}
	//public void UpDown()
	//{
	//	//if (UnityEngine.Input.GetKey(KeyCode.UpArrow))
	//	//{
	//		isDragging = true;
	//		temPos = base.transform.localPosition;
	//		temPos = new Vector3(temPos.x, temPos.y + 100f * inverseScreenScale.y, 0f);
	//		currentDistance = Vector2.SqrMagnitude(temPos - originPos);
	//		if (currentDistance > radius * radius)
	//		{
	//			temPos -= originPos;
	//			Vector3 normalized = temPos.normalized;
	//			base.transform.localPosition = normalized * radius + originPos;
	//		}
	//		else
	//		{
	//			base.transform.localPosition = temPos;
	//		}
 //       wxOnDragStart();
 //       //if (!flag)
 //       //{
 //       //    wxOnDragStart();
 //       //}
 //       //}
 //   }
	//public void DownDown()
	//{
 //       //if (UnityEngine.Input.GetKey(KeyCode.DownArrow))
 //       //{
 //           isDragging = true;
 //           temPos = base.transform.localPosition;
 //           temPos = new Vector3(temPos.x, temPos.y - 100f * inverseScreenScale.y, 0f);
 //           currentDistance = Vector2.SqrMagnitude(temPos - originPos);
 //           if (currentDistance > radius * radius)
 //           {
 //               temPos -= originPos;
 //               Vector3 normalized2 = temPos.normalized;
 //               base.transform.localPosition = normalized2 * radius + originPos;
 //           }
 //           else
 //           {
 //               base.transform.localPosition = temPos;
 //           }
 //       wxOnDragStart();
 //       //if (!flag)
 //       //{
 //       //    wxOnDragStart();
 //       //}
 //       //}

 //   }
 //   public void LeftDown()
	//{
 //       //if (UnityEngine.Input.GetKey(KeyCode.LeftArrow))
 //       //{
 //           isDragging = true;
 //           temPos = base.transform.localPosition;
 //           temPos = new Vector3(temPos.x - 100f * inverseScreenScale.x, temPos.y, 0f);
 //           currentDistance = Vector2.SqrMagnitude(temPos - originPos);
 //           if (currentDistance > radius * radius)
 //           {
 //               temPos -= originPos;
 //               Vector3 normalized3 = temPos.normalized;
 //               base.transform.localPosition = normalized3 * radius + originPos;
 //           }
 //           else
 //           {
 //               base.transform.localPosition = temPos;
 //           }
 //       wxOnDragStart();
 //       //if (!flag)
 //       //{
 //       //    wxOnDragStart();
 //       //}
 //       //}

 //   }
 //   public void RightDown()
	//{
 //       //if (UnityEngine.Input.GetKey(KeyCode.RightArrow))
 //       //{
 //           isDragging = true;
 //           temPos = base.transform.localPosition;
 //           temPos = new Vector3(temPos.x + 100f * inverseScreenScale.x, temPos.y, 0f);
 //           currentDistance = Vector2.SqrMagnitude(temPos - originPos);
 //           if (currentDistance > radius * radius)
 //           {
 //               temPos -= originPos;
 //               Vector3 normalized4 = temPos.normalized;
 //               base.transform.localPosition = normalized4 * radius + originPos;
 //           }
 //           else
 //           {
 //               base.transform.localPosition = temPos;
 //           }
 //       wxOnDragStart();
 //       //if (!flag)
 //       //{
 //       //    wxOnDragStart();
 //       //}
 //       //}
 //   }
}
